
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Jacob
 */
public class Bin2Img {
    private static int pointerX = 0;
    private static int pointerY = 0;
    private static int width;
    private static int height;
    private static Graphics graphics;
    public static int size;
    private static void incPointer()
    {
        pointerX++;
        if(pointerX >= width)
        {
            pointerX = 0;
            pointerY++;
        }
    }
    private static void put(int r, int g, int b)
    {
        Color c = new Color(r, g, b);
        graphics.setColor(c);
        graphics.drawLine(pointerX, pointerY, pointerX, pointerY);
        incPointer();
    }
    public static BufferedImage doConvert(File inputFile)
    {
        try
        {
            FileInputStream is = new FileInputStream(inputFile);

            size = is.available();
            int s = (int) Math.sqrt(size/3)+2;
            width = height = s;
            int pos = 0;
            int[] bytes = new int[size];
            int bt;
            while((bt = is.read()) != -1)
            {
                bytes[pos] = bt;
                pos++;
            }
            is.close();

            BufferedImage im = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            graphics = im.getGraphics();
            graphics.setColor(new Color(124, 124, 124));
            graphics.fillRect(0, 0, width, height);
            
            int ptr = 0;
            while(ptr < size)
            {
                int r;
                int g;
                int b;
                r = bytes[ptr];
                ptr++;
                if(ptr < size)
                    g = bytes[ptr];
                else
                    g = 0;
                ptr++;
                if(ptr < size)
                    b = bytes[ptr];
                else
                    b = 0;
                ptr++;
                put(r, g, b);
            }
            return im;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return null;
        }
    }
}
